package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired

    private DishFlavorMapper dishFlavorMapper;


    //菜品列表
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //设置条件
        LambdaQueryWrapper<Dish> qw1 = new LambdaQueryWrapper<>();
        qw1.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        //构建分页
        Page<Dish> Page = new Page<>(pageNum, pageSize);
        //加入条件进行分页查询
        Page = dishMapper.selectPage(Page, qw1);

        for (Dish dish : Page.getRecords()) {
            //分类名称查询    select * from category where id =
            //从dishpage 得到分类id 分类id拿到分类表去查分类的值 并set到当前值里面去
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());

            //口味列表查询  select * from dish_flavor where dish_id =
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId, dish.getId());
            // where dish_id = 1397850392090947585
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw2);
            dish.setFlavors(flavorList);


        }


        //返回查到东西
        return Page;
    }

    //按type查询
    @Override
    public List<Category> findBytype(Integer type) {
        //构建条件
        LambdaQueryWrapper<Category> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(Category::getType, type);
        //按条件查询
        List<Category> categories = categoryMapper.selectList(qw1);
        return categories;
    }

    //新增
    @Override
    public ResultInfo save(Dish dish) {

        //把新建的存进去
        dishMapper.insert(dish);
        //把口味表和菜品表关联
        List<DishFlavor> flavors = dish.getFlavors();

        for (DishFlavor flavor : flavors) {
            //两个表建立链接
            flavor.setDishId(dish.getId());
            //存入 2-2 保存数据库  insert into dish_flavor() values();
            dishFlavorMapper.insert(flavor);
        }

        return null;
    }

    //查询回显
    @Override
    public Dish findByid(long dishId) {

        Dish dish = dishMapper.selectById(dishId);
        // 2.构建口味查询条件
        // select * from dish_flavor where dish_id = 1632199055591378946
        LambdaQueryWrapper<DishFlavor> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(DishFlavor::getDishId, dishId);
        //查询口味表
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw1);
        //将口味封装到菜品
        dish.setFlavors(dishFlavors);

        return dish;
    }

    @Override
    public ResultInfo updateDish(Dish dish) {
        //修改菜品
        dishMapper.updateById(dish);


        //删除原有的口味
        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        //获取原有的口味id
        qw.eq(DishFlavor::getDishId, dish.getId());
        //按获得的id删除
        dishFlavorMapper.delete(qw);

        //获取传入的口味
        List<DishFlavor> flavors = dish.getFlavors();

        //之前的id删除了 现在要添加新的了

        for (DishFlavor flavor : flavors) {
            // 3-1 口味关联菜品id
            flavor.setDishId(dish.getId());
            dishFlavorMapper.insert(flavor);
        }


        return ResultInfo.success(dish);
    }

    //起售 停售
    @Override
    public ResultInfo statusDish(Integer status, List<Long> list) {
        //判断他是否为空
        if (CollUtil.isNotEmpty(list)) {
            for (Long dishId : list) {
                Dish dish = new Dish();
                //修改菜品状态
                dish.setStatus(status);
                //修改需要修改的id
                dish.setId(dishId);
                dishMapper.updateById(dish);
            }

        }


        return ResultInfo.success();
    }

    //菜品删除
    @Override
    public ResultInfo deleByid(List<Long> ids) {
        //先查询 id符合 而且状态是起售的id
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getStatus, 1);//获得状态为1的
        qw.in(Dish::getId, ids);//符合要求的

        //算一下有没有状态是起售的  没有是起售就执行删除
        Integer dishcount = dishMapper.selectCount(qw);
        if (dishcount > 0) {
            throw new CustomException("有起售的商品 删除删除失败");
        }

        //集合里没有是起售的就执行删除
        dishMapper.deleteBatchIds(ids);

        // 4.删除菜品对应口味  delete from dish_flavor where dish_id in (1632199055591378946,1413384757047271425)
        LambdaQueryWrapper<DishFlavor> flavorQw = new LambdaQueryWrapper<>();
        flavorQw.in(DishFlavor::getDishId,ids);  // where dish_id in (1632199055591378946,1413384757047271425)
        dishFlavorMapper.delete(flavorQw);

        return ResultInfo.success();

    }
    //把菜品封装到套餐里面
    @Override
    public List<Dish> list(Long categoryId, Integer status) {
        // 1.查询菜品 select * from dish where category_id = ? and status = ?
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        List<Dish> dishList = dishMapper.selectList(qw);

        // 2.遍历菜品列表
        dishList.forEach(dish -> {
            // 3.根据菜品id查询口味列表 select * from dish_flavor where dish_id = ?
            LambdaQueryWrapper<DishFlavor> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> flavorList = dishFlavorMapper.selectList(qw2);
            // 封装到菜品中
            dish.setFlavors(flavorList);
        });

        // 返回菜品列表
        return dishList;
    }
}
